package cn.bellychang.alibaba;

/**
 * @author ChangLiang
 * @date 2021/3/18
 */
public class Solution2 {

    public String getMaxRepeatingSubString(String str) {

        char[] array = str.toCharArray();
        int count = 1;
        int max = 0;
        char maxChar = 0;
        int maxIndex = 0;

        // Start from 1 since we want to compare it with the char in index 0
        for (int i = 1; i < array.length; i++) {
            if (array[i] == array[i - 1]) {
                count++;
            } else {
                // Record current run length, is it the maximum?
                if (count > max) {
                    max = count;
                    maxChar = array[i - 1];
                    maxIndex = i - 1;
                }
                // Reset the count
                count = 1;
            }
        }
        if (count > max) {
            // This is to account for the last run
            max = count;
            maxChar = array[array.length - 1];
            maxIndex = array.length - 1;
        }
        System.out.println("Longest run: " + max + ", for the character " + maxChar + "and the max char index is:" + maxIndex);
        return str.substring(maxIndex - (max - 1), maxIndex + 1);
    }
}
